package com.xzx.study;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MotionActivity extends AppCompatActivity {
    private static final String TAG = "TAG-" + MotionActivity.class.getSimpleName();

    private ImageView imageView;
    private int lastX;
    private int lastY;
    private int eventX;
    private int eventY;
    private int dx;
    private int dy;

    private LinearLayout container;
    private int maxRight;
    private int maxBottom;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motion);

        container = findViewById(R.id.event_container);// 这里获取父视图的宽高会为0

        imageView = findViewById(R.id.motion_event);
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i(TAG, "event:" + event.getAction());
                eventX = (int) event.getRawX();
                eventY = (int) event.getRawY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = eventX;
                        lastY = eventY;
                        maxRight = container.getRight();
                        maxBottom = container.getBottom();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        dx = eventX - lastX;
                        dy = eventY - lastY;
                        int left = imageView.getLeft() + dx;
                        int top = imageView.getTop() + dy;
                        int right = imageView.getRight() + dx;
                        int bottom = imageView.getBottom() + dy;
                        if (left < 0) {
                            right = right - left;
                            left = 0;
                        }
                        if (top < 0) {
                            bottom = bottom - top;
                            top = 0;
                        }
                        if (right > maxRight)  {
                            left = left - (right - maxRight);
                            right = maxRight;
                        }
                        if (bottom > maxRight) {
                            top = top -  (bottom - maxBottom);
                            bottom = maxBottom;
                        }
                        imageView.layout(left, top, right, bottom);
                        lastX = eventX;
                        lastY = eventY;
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
}
